package com.yzt.act.listener;

import java.util.Arrays;
import java.util.List;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

import com.yzt.act.entity.CommAssignee;
import com.yzt.act.service.ICommAssigneeService;
import com.yzt.base.entity.BaseUser;
import com.yzt.base.utils.SecurityUtils;
import com.yzt.base.utils.SpringUtil;

public class LeaveTaskListener implements TaskListener {

	private static final long serialVersionUID = 1L;

	private ICommAssigneeService commAssigneeService;

	public LeaveTaskListener() {
		this.commAssigneeService = SpringUtil.getBean(ICommAssigneeService.class);
	}

	@Override
	public void notify(DelegateTask delegateTask) {
		System.out.println(delegateTask.getTaskDefinitionKey());
		System.out.println(delegateTask.getProcessDefinitionId());

		CommAssignee assignee = commAssigneeService.getAssignee(delegateTask.getProcessDefinitionId(),
				delegateTask.getTaskDefinitionKey());
		if (assignee != null) {
			if ("1".equals(assignee.getType())) {// 执行人设置
				delegateTask.setAssignee(assignee.getAssignee());
			} else if ("2".equals(assignee.getType())) {// 候选人设置
				List<String> users = Arrays.asList(assignee.getAssignee().split(","));
				delegateTask.addCandidateUsers(users);
			} else if ("3".equals(assignee.getType())) {// 候选组设置
				List<String> groups = Arrays.asList(assignee.getRolegroup().split(","));
				delegateTask.addCandidateGroups(groups);
			}
		} else {
			BaseUser user = SecurityUtils.getUser();
			delegateTask.setAssignee(user.getUsername());
		}

	}

}
